<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 11.2.&nbsp; Date and Time Functions</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-11-SECT-1.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-11-SECT-3.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top"><a name="learnphpmysql-CHP-11-SECT-2"></a>
<h3 id="title-IDAOI0HQ" class="docSection1Title">11.2. Date and Time Functions</h3>
<a name="IDX-CHP-11-0537"></a> 
<a name="IDX-CHP-11-0538"></a> 

<p class="docText">PHP uses the standard Unix-style timestamp to work with dates. This is simply the number of seconds since January 1, 1970. You get the current timestamp using the <tt>time</tt> function, shown in <a class="docLink" href="#learnphpmysql-CHP-11-EX-15">Example 11-15</a>.</p>
<a name="learnphpmysql-CHP-11-EX-15"></a><h5 id="title-IDAHJ0HQ" class="docExampleTitle">Example 11-15. A simple echo of the timestamp</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
$timestamp= time();
echo $timestamp;
?&gt;
</pre><br>

</td></tr></table></p>
<p class="docText">This results in <a class="docLink" href="#learnphpmysql-CHP-11-FIG-13">Figure 11-13</a>.</P>
<a name="learnphpmysql-CHP-11-FIG-13"></a><p><center>
<h5 class="docFigureTitle">Figure 11-13. A Unix timestamp</H5>
<img border="0" alt="" width="506" height="183" SRC="images/learnphpmysql_1113.jpg">
</center></P><br>
<pre>
1134511981
</pre><BR>

<p class="docText">This is not exactly the most meaningful representation of the date and time. So instead, you can use the <tt>date</tt> function to translate the timestamp into a meaningful string. The <tt>date</tt> function takes a timestamp and a format string, as shown in <a class="docLink" href="#learnphpmysql-CHP-11-EX-16">Example 11-16</a>.</p>
<a name="learnphpmysql-CHP-11-EX-16"></a><h5 id="title-IDASK0HQ" class="docExampleTitle">Example 11-16. Making the date and time appear like we expect</h5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
$timestamp= time();
echo date("m/d/y G.i:s",$timestamp);
?&gt;
</pre><BR>

</td></tr></table></P>
<p class="docText">This code returns the screen shown in <a class="docLink" href="#learnphpmysql-CHP-11-FIG-14">Figure 11-14</a>.</p>
<a name="learnphpmysql-CHP-11-FIG-14"></a><p><center>
<h5 class="docFigureTitle">Figure 11-14. An easy-to-read date and time from the date function</h5>
<img border="0" alt="" width="506" height="183" SRC="images/learnphpmysql_1114.jpg">
</center></P><BR>
<p class="docText">Dates and times can be displayed in a variety of formats; these will be discussed next.</p>
<a name="learnphpmysql-CHP-11-SECT-2.1"></a>
<h4 id="title-IDARL0HQ" class="docSection2Title">11.2.1. Display Formats</h4>
<a name="IDX-CHP-11-0539"></a> 
<a name="IDX-CHP-11-0540"></a> 
<a name="IDX-CHP-11-0541"></a> 

<p class="docText">Date and times are displayed in a variety of formats.<a name="IDX-CHP-11-0542"></a> 
<a name="IDX-CHP-11-0543"></a> 
 In <a class="docLink" href="#learnphpmysql-CHP-11-EX-16">Example 11-16</a>, we used a date format string of <tt>m/d/y G.i:s</tt>. <a class="docLink" href="#learnphpmysql-CHP-11-TABLE-2">Table 11-2</a> shows other possible components for those formats.</p>
<a name="learnphpmysql-CHP-11-TABLE-2"></a><P><table cellspacing="0" FRAME="hsides" RULES="all" cellpadding="4" width="100%"><caption><h5 class="docTableTitle">Table 11-2. Time-formatting values</H5></caption><colgroup span="3"><col><col><col></colgroup><thead><tr><th class="thead" scope="col" align="left"><p class="docText">Format</P></th><th class="thead" scope="col" align="left"><p class="docText">Meaning</P></th><th class="thead" scope="col" align="left"><p class="docText">Example value</P></th></tr></thead><tr><td class="docTableCell" align="left"><p class="docText">a</p></td><td class="docTableCell" align="left"><p class="docText">am or pm</p></td><td class="docTableCell" align="left"><p class="docText">Am</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText">A</P></td><td class="docTableCell" align="left"><p class="docText">AM or PM</P></TD><td class="docTableCell" align="left"><p class="docText">AM</P></td></tr><tr><TD class="docTableCell" align="left"><p class="docText">d</p></td><TD class="docTableCell" align="left"><p class="docText">Day of the month</p></td><TD class="docTableCell" align="left"><p class="docText">01</p></td></tr><tr><TD class="docTableCell" align="left"><p class="docText">D</P></td><td class="docTableCell" align="left"><p class="docText">Day or the week</p></td><TD class="docTableCell" align="left"><p class="docText">Sun</p></TD></tr><TR><TD class="docTableCell" align="left"><p class="docText">F</P></td><td class="docTableCell" align="left"><p class="docText">Month name</p></td><td class="docTableCell" align="left"><p class="docText">January</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText">h</p></td><td class="docTableCell" align="left"><p class="docText">Hours in 12-hour format with leading zeros</p></TD><td class="docTableCell" align="left"><p class="docText">04</p></TD></TR><tr><TD class="docTableCell" align="left"><p class="docText">H</p></td><td class="docTableCell" align="left"><p class="docText">Hours in 24-hour format with leading zeros</P></td><td class="docTableCell" align="left"><p class="docText">16</P></td></tr><TR><td class="docTableCell" align="left"><p class="docText">g</p></td><td class="docTableCell" align="left"><p class="docText">Hours in 12-hour format without leading zeros</P></TD><td class="docTableCell" align="left"><p class="docText">4</p></td></tr><TR><td class="docTableCell" align="left"><p class="docText">G</P></td><TD class="docTableCell" align="left"><p class="docText">Hours in 24-hour format without leading zeros</P></TD><td class="docTableCell" align="left"><p class="docText">16</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText">i</p></td><td class="docTableCell" align="left"><p class="docText">Minutes</p></td><td class="docTableCell" align="left"><p class="docText">35</p></td></TR><tr><td class="docTableCell" align="left"><p class="docText">j</P></TD><td class="docTableCell" align="left"><p class="docText">Day of the month</P></td><td class="docTableCell" align="left"><p class="docText">2</p></TD></tr><tr><TD class="docTableCell" align="left"><p class="docText">l</p></td><TD class="docTableCell" align="left"><p class="docText">Day of the week as a name</p></td><td class="docTableCell" align="left"><p class="docText">Sunday</p></TD></TR><tr><td class="docTableCell" align="left"><p class="docText">L</p></td><TD class="docTableCell" align="left"><p class="docText">Leap year (1 for yes, 0 for no)</p></TD><td class="docTableCell" align="left"><p class="docText">1</P></TD></TR><tr><td class="docTableCell" align="left"><p class="docText">m</p></td><td class="docTableCell" align="left"><p class="docText">Month of the year abbreviated to three characters</p></td><td class="docTableCell" align="left"><p class="docText">Jul</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText">M</p></TD><td class="docTableCell" align="left"><p class="docText">Month of the year</p></TD><TD class="docTableCell" align="left"><p class="docText">July</p></TD></tr><tr><td class="docTableCell" align="left"><p class="docText">N</P></td><td class="docTableCell" align="left"><p class="docText">Month of the year as number without leading zeros</P></td><td class="docTableCell" align="left"><p class="docText">7</P></td></tr><tr><td class="docTableCell" align="left"><p class="docText">s</P></TD><td class="docTableCell" align="left"><p class="docText">Seconds of the hour</p></td><td class="docTableCell" align="left"><p class="docText">58</P></td></TR><tr><TD class="docTableCell" align="left"><p class="docText">S</P></TD><td class="docTableCell" align="left"><p class="docText">Suffix for the day</p></td><td class="docTableCell" align="left"><p class="docText">th, nd, st, rd</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText">R</p></td><td class="docTableCell" align="left"><p class="docText">Standardized date format</p></td><TD class="docTableCell" align="left"><p class="docText">Thu, 15 Dec 2005 16:49:39-0600</p></td></TR><TR><td class="docTableCell" align="left"><p class="docText">U</P></td><td class="docTableCell" align="left"><p class="docText">Timestamp</p></TD><td class="docTableCell" align="left"><p class="docText">1134512479</p></TD></tr><tr><TD class="docTableCell" align="left"><p class="docText">y</p></td><td class="docTableCell" align="left"><p class="docText">Two-digit year</p></TD><TD class="docTableCell" align="left"><p class="docText">25</p></td></tr><tr><TD class="docTableCell" align="left"><p class="docText">Y</p></TD><td class="docTableCell" align="left"><p class="docText">Four-digit year</P></TD><TD class="docTableCell" align="left"><p class="docText">2025</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText">z</p></td><td class="docTableCell" align="left"><p class="docText">Day of year</p></td><td class="docTableCell" align="left"><p class="docText">234</p></td></tr><TR><td class="docTableCell" align="left"><p class="docText">Z</p></TD><TD class="docTableCell" align="left"><p class="docText">GMT offset in seconds (Greenwich Mean Time)</p></TD><td class="docTableCell" align="left"><p class="docText">-21600 (-6*60*60)</p></td></TR></table></p><br>

<a name="learnphpmysql-CHP-11-SECT-2.2"></a>
<H4 id="title-IDA4Z0HQ" class="docSection2Title">11.2.2. Arithmetic</h4>
<p class="docText">Adding or subtracting days and hours can be done by adding or subtracting seconds. While this may sound odd, it's not hard. To add two days to a timestamp, add 2*24*60*60 (2 days*24 hours*60 minutes*60 seconds) to the timestamp, as shown in <a class="docLink" href="#learnphpmysql-CHP-11-EX-17">Example 11-17</a>.</p>
<a name="learnphpmysql-CHP-11-EX-17"></a><H5 id="title-IDAM00HQ" class="docExampleTitle">Example 11-17. Adding two days to the date</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
$timestamp= time();
echo date("m/d/y G.i:s",$timestamp);
$seconds=2*24*60*60;
$timestamp+=$seconds;
echo "&lt;br&gt;new dates<a name="IDX-CHP-11-0544"></a> 
 is:";
echo date("m/d/y G.i:s",$timestamp);
?&gt;
</pre><BR>

</TD></tr></table></p>
<p class="docText">This outputs:</p>
<pre>
12/13/05 16.28:32
new dates is:12/15/05 16.28:32
</pre><br>

<p class="docText">Let's see what else you need to create dates with validation.</P>

<a name="learnphpmysql-CHP-11-SECT-2.3"></a>
<h4 id="title-IDAC10HQ" class="docSection2Title">11.2.3. Validating Dates</H4>
<a name="IDX-CHP-11-0545"></a> 
<a name="IDX-CHP-11-0546"></a> 

<p class="docText">When you receive a user-supplied date, it's good practice, as with any other user-supplied data, to check that it's valid. You can use the <tt>checkdate</tt> function, shown in <a class="docLink" href="#learnphpmysql-CHP-11-EX-18">Example 11-18</a>, to validate a date. It takes three parametersthe month, day, and yearfor a date to validate. If the date is valid, it returns <tt>trUE</tt>; otherwise, it returns <tt>FALSE</tt>.</P>
<a name="learnphpmysql-CHP-11-EX-18"></a><H5 id="title-IDAB20HQ" class="docExampleTitle">Example 11-18. Validating two dates</H5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
  echo("Validating: 4/31/2005&lt;br&gt;");
  if (checkdate(4,31,2005)) {
    echo('Date accepted.');
  }
  else {
    echo ('Invalid date.');
  }
  echo("&lt;br&gt;");
  echo("Validating: 5/31/2005&lt;br&gt;");
  if (checkdate(5,31,2005)) {
    echo('Date accepted.');
  }
  else {
    echo ('Invalid date.');
  }
?&gt;
</pre><br>

</td></tr></table></p>
<p class="docText">As you can tell by our example in <a class="docLink" href="#learnphpmysql-CHP-11-FIG-15">Figure 11-15</a>, the 31 April 2005 date was invalid, yet 31 May 2005 was valid. This can happen because of a typo or a user just entering wrong information.</p>
<a name="learnphpmysql-CHP-11-FIG-15"></a><p><center>
<h5 class="docFigureTitle">Figure 11-15. Only some months have 31 days</h5>
<img border="0" alt="" width="496" height="156" SRC="images/learnphpmysql_1115.jpg">
</center></p><br>
<p class="docText">Once you know that you have the valid segments of a date, you can create a timestamp.</p>

<a name="learnphpmysql-CHP-11-SECT-2.4"></a>
<H4 id="title-IDAB30HQ" class="docSection2Title">11.2.4. Using mktime to Create a Timestamp</h4>
<a name="IDX-CHP-11-0547"></a> 
<a name="IDX-CHP-11-0548"></a> 
<a name="IDX-CHP-11-0549"></a> 
<a name="IDX-CHP-11-0550"></a> 
<a name="IDX-CHP-11-0551"></a> 

<p class="docText">It's fairly easy to get the current time and date using <tt>date</tt>, but if you're trying to create a date and all you have are the components of the date such as month, day, and year, you'll need to use the <tt>mktime</tt> function. The <tt>mktime</tt> function takes these parameters:</p>
<UL><LI><p class="docList">Hour</p></LI><li><p class="docList">Minute</p></li><LI><p class="docList">Second</p></li><LI><p class="docList">Month</p></li><LI><p class="docList">Day of the month</p></li><li><p class="docList">Year</p></LI></UL>
<p class="docText">You can omit some of the parameters when calling <tt>mktime</tt>, and they'll be filled in from the current time. <tt>mktime</tt> is a timestamp, which is an integer containing the number of seconds between the Unix Epoch of January 1 1970 00:00:00 GMT and the time specified. You can't omit them out of order though. <a class="docLink" href="#learnphpmysql-CHP-11-EX-19">Example 11-19</a> checks whether the date is valid, and then creates a timestamp.</p>
<a name="learnphpmysql-CHP-11-EX-19"></a><h5 id="title-IDAK50HQ" class="docExampleTitle">Example 11-19. Creating a timestamp from the components of a date</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><TR><td>

<pre>
&lt;?php
  echo("Validating: 5/31/2005&lt;br&gt;");
  if (checkdate(5,31,2005)) {
    echo('Date accepted: ');
    $new_date=mktime(18,05,35,5,31,2005);
    echo date("r",$new_date);
  }
  else {
    echo ('Invalid date.');
  }
  echo("&lt;br&gt;");
?&gt;
</pre><BR>

</td></TR></table></P>
<p class="docText">When run, this code produces the screen shown in <a class="docLink" href="#learnphpmysql-CHP-11-FIG-16">Figure 11-16</a>.</P>
<a name="learnphpmysql-CHP-11-FIG-16"></a><p><center>
<h5 class="docFigureTitle">Figure 11-16. A timestamp created from its components</h5>
<img border="0" alt="" width="546" height="165" SRC="images/learnphpmysql_1116.jpg">
</center></p><br>
<p class="docText">Now that we've covered dates and times, it's time to head onto more exciting topics. Note that the number of the year might be a two- or four-digit value, between 0 to 69 mapping to 2000 to 2069 and 70 to 100 to 1970 to 2000. On systems where <tt>time_t</tt> is a 32-bit signed integer, which is most common today, the valid range for the year is somewhere between 1901 and 2038. This limitation is fixed since the release of PHP 5.1.0.</p>


</TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-11-SECT-1.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-11-SECT-3.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</html>
